package ms.unicorn.erml.helper;

import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;

import ms.unicorn.Element;
import ms.unicorn.erml.edit.parts.RequirementEditPart;
import ms.unicorn.modellanguage.LanguageEntityEditHeperAdvice;
import ms.unicorn.ui.figures.EntityRoundRectangle;
import ms.unicorn.ui.layout.DockConstraint;

public class RequirementEditHelperAdvice extends LanguageEntityEditHeperAdvice {
	
	public RequirementEditHelperAdvice() {
		editPartMap.put(ENTITY_ID, RequirementEditPart.class);
	}
	
	@Override
	protected IFigure createFigureElement(Element element,
			String figureElement, EditPart elementEditPart) {
		if (FIGURE_ELEMENT_PRIMARY_SHAPE.equals(figureElement))
			return new EntityRoundRectangle();
		if (FIGURE_ELEMENT_STEREOTYPE.equals(figureElement)) {
			WrappingLabel label = (WrappingLabel) super.createFigureElement(element, 
											figureElement, elementEditPart);
			label.setTextJustification(PositionConstants.CENTER);
			label.setTextAlignment(PositionConstants.CENTER);
			label.setAlignment(PositionConstants.CENTER);
			return label;
		}
		if (FIGURE_ELEMENT_NAME.equals(figureElement)) {
			WrappingLabel label = (WrappingLabel) super.createFigureElement(element, 
					figureElement, elementEditPart);
			label.setTextJustification(PositionConstants.CENTER);
			label.setTextAlignment(PositionConstants.CENTER);
			label.setAlignment(PositionConstants.CENTER);
			label.setTextWrap(true);
			return label;
		}
		return super.createFigureElement(element, figureElement, elementEditPart);
	}
	
	@Override
	protected void buildFigures(EditPart context, Element element,
			EditPart elementEditPart, Map<String, IFigure> figures) {
		IFigure figure = figures.get(FIGURE_ELEMENT_PLATE);
		IFigure primaryShape = figures.get(FIGURE_ELEMENT_PRIMARY_SHAPE);
		IFigure nameFigure = figures.get(FIGURE_ELEMENT_NAME);
		IFigure stereotype = figures.get(FIGURE_ELEMENT_STEREOTYPE);
		
		//figure.add(primaryShape, BorderLayout.CENTER);
		//figure.add(nameFigure, BorderLayout.BOTTOM);
		figure.add(primaryShape, new DockConstraint(DockConstraint.DOCK_FILL, 0, 0));
		if (stereotype != null)
			figure.add(stereotype, new DockConstraint(DockConstraint.DOCK_TOP, 0, 20));
		if (nameFigure != null)
			figure.add(nameFigure, new DockConstraint(DockConstraint.DOCK_FILL, 0, 20));
		
	}
}
